
//
//  EquipmentListTableViewCell.m
//  Sampux
//
//  Created by rzm on 16/7/5.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxListTableViewCell.h"

@implementation SampuxListTableViewCell

- (instancetype)initWithFrame:(CGRect)frame

{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = THSystemClearColor;
        [self setupSubView];
    }
    return self;
}


- (void)setupSubView
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 16.;
    }
    
    _redView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 4, Sampux6HScale(64))];
    [self addSubview:_redView];
    
    _equipmentName = [UILabel createLabel];
    _equipmentName.frame = CGRectMake(Sampux6WScale(15), Sampux6HScale(10), Sampux6WScale(100), Sampux6HScale(30));
    [self addSubview:_equipmentName];
    _equipmentName.font = THFont(font);
    _equipmentName.textColor = THWhiteAlplaColor(0.8);
    
    _shutdownOrNotImg = [[UIImageView alloc]initWithFrame:CGRectMake(_equipmentName.right + 5, _equipmentName.y + 5, Sampux6WScale(30), Sampux6HScale(35/2))];
    [self addSubview:_shutdownOrNotImg];
    
    _isOffLineOrNotLab = [UILabel createLabel];
    _isOffLineOrNotLab.frame = CGRectMake(Sampux6WScale(15), _equipmentName.bottom, Sampux6HScale(40), Sampux6HScale(20));
    [self addSubview:_isOffLineOrNotLab];
    _isOffLineOrNotLab.textColor = THWhiteAlplaColor(0.8);
    _isOffLineOrNotLab.font = THFont(font - 2);
    
    _stateImg = [[UIImageView alloc]init];
    [self addSubview:_stateImg];
    _stateImg.frame = CGRectMake(_isOffLineOrNotLab.right, _equipmentName.bottom + 5 , Sampux6HScale(13), Sampux6HScale(11));
    _stateImg.userInteractionEnabled = NO;
    
    _heatingCondition = [[UIImageView alloc]init];
    _heatingCondition.frame = CGRectMake(_stateImg.right + Sampux6WScale(5), _stateImg.y, Sampux6HScale(10), Sampux6HScale(11));
    [self addSubview:_heatingCondition];
    _heatingCondition.userInteractionEnabled = NO;
    
    _arrowBtn = [[UIImageView alloc]init];
    _arrowBtn.frame = CGRectMake(kScreenWidth -  Sampux6WScale(10) - Sampux6HScale(14) - Sampux6WScale(15), Sampux6HScale(65)/2 - Sampux6HScale(14)/2, Sampux6WScale(7), Sampux6HScale(14));
    _arrowBtn.image = imageNamed(@"arrow-1");

    [self addSubview:_arrowBtn];
    
    _timeLab = [UILabel createLabelWithFrame:CGRectMake(_arrowBtn.left - Sampux6WScale(60) - 5 - Sampux6HScale(10), 10, Sampux6WScale(60), Sampux6HScale(24))];
    [self addSubview:_timeLab];
    _timeLab.textAlignment = NSTextAlignmentRight;
    _timeLab.font = THFont(font);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
    _timeLab.text = currentDateStr;
    _timeLab.textColor = THWhiteAlplaColor(0.8);

    _tempLab = [UILabel createLabelWithFrame:CGRectMake(_arrowBtn.left - Sampux6WScale(40) - 5 - Sampux6HScale(10), _timeLab.bottom + Sampux6HScale(5), Sampux6WScale(40), Sampux6HScale(20))];
    [self addSubview:_tempLab];
    _tempLab.textAlignment = NSTextAlignmentRight;
    _tempLab.text = @"25℃";
    _tempLab.font = THFont(font - 2);
    _tempLab.textColor = THWhiteAlplaColor(0.8);
}

- (void)setListModel:(SampuxListModel *)listModel
{
    _listModel = listModel;
    
    if (_listModel.nameStr.length == 0) {
        self.nameStr = _listModel.accountStr;
    }else
    {
        self.nameStr = _listModel.nameStr;
     }
    if (_listModel.currentRoomTempe == 10000) {
        self.tempStr = @"E2";
        _tempLab.textColor = THSystemRedColor;
    }else
    {
        self.tempStr = [NSString stringWithFormat:@"%.f℃",_listModel.currentRoomTempe];
    }
    
    if (_listModel.isShutDownOrNot == NO) {
        _shutdownOrNotImg.image = imageNamed(@"boot_list");
    }else
    {
        _shutdownOrNotImg.image = imageNamed(@"shutdown_rec");
    }
    
    if (_listModel.isOnLine == NO) {
        _isOffLineOrNotLab.text = @"离线";
    }else
    {
        _isOffLineOrNotLab.text = @"在线";
    }
    
    NSArray *heatArr = @[@"heat_state_yes",@"heat_state_no"];
    NSArray *stateArr = @[@"energy_list_on",@"fasthot_list_on",@"comfortable_list_on"];
    if (_listModel.isOnLine == YES) {
        self.stateImg.hidden = NO;
        self.tempLab.hidden = NO;
        self.shutdownOrNotImg.hidden = NO;
        self.heatingCondition.hidden = NO;
        
        if (_listModel.isHeat == YES) {
            self.heatImgStr = heatArr[0];
        }else
        {
            self.heatImgStr = heatArr[1];
        }
        if (_listModel.heatMode == 1) {
            self.stateImgStr = @"fasthot_list_on";
        }else if (_listModel.heatMode == 2)
        {
            self.stateImgStr = @"comfortable_list_on";
        }else
        {
            self.stateImgStr = @"energy_list_on";
        }
    }else
    {
        self.stateImg.hidden = YES;
        self.heatingCondition.hidden = YES;
        self.shutdownOrNotImg.hidden = YES;
        self.tempLab.hidden = YES;
    }
}

- (void)setHeatImgStr:(NSString *)heatImgStr
{
    _heatImgStr = heatImgStr;
    _heatingCondition.image = imageNamed(heatImgStr);
}

- (void)setStateImgStr:(NSString *)stateImgStr
{
    _stateImgStr = stateImgStr;
    _stateImg.image = imageNamed(_stateImgStr);
}

- (void)setNameStr:(NSString *)nameStr
{
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 16.;
    }
    _nameStr = nameStr;
    _equipmentName.text = _nameStr;
    CGSize maxSize = CGSizeMake(MAXFLOAT,Sampux6HScale(30));
    CGSize strSize = [SampuxGlobalTool getSizeWithString:_nameStr andFont:THFont(font) andMaxSize:maxSize];
    _equipmentName.width = strSize.width;
    _shutdownOrNotImg.x = _equipmentName.right + 5;
}

- (void)setTempStr:(NSString *)tempStr
{
    _tempStr = tempStr;
    _tempLab.text = _tempStr;
}

- (void)setTimeStr:(NSString *)timeStr
{
    _timeStr = timeStr;
    _timeLab.text = _timeStr;
}

@end
